<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : shopcategory.class.php   FILE_PATH : \\classes\application\shopcategory.class.php
 * ....商店分类管理类
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Thu Sep 06 17:52:39 CST 2007
 */

class ShopCategoryClass extends FrameWork {
	/**
	 * 构造方法
	 * @author workflow Develop Team     
	 * @param  初始化语言包，为其他有可能出现递归的方法方便替换语言
	 * @return int/bool/object/array/string
	 */
	function ShopCategoryClass(){
		$this->__construct();
	}
	function __construct(){
		//初始化变量
		$this->_getConfiginfo();
		//加载语言包
		$this->getlang("sys_shopcategory.manage");
	}
	
	/**
	 * 新增商店分类
	 *
	 * @param var[] $input_param
	 * @return boolen
	 */
	function addCategory($input_param){
		$valueArray = array();
		$valueArray['class_name'] = $input_param['txtClassName'];
		$valueArray['class_state'] = intval($input_param['rdoClassState']);
		$valueArray['class_sort'] = intval($input_param['txtClassSort']);
		$valueArray['class_property'] = intval($input_param['rdoClassProperty']);
		$valueArray['parent_id'] = $input_param['slcParentId'];
		return $GLOBALS['db']->InsertRow($valueArray, 'shop_class', 'class_id');
	}

	/**
	 * 操作商铺分类
	 *
	 * @param var[] $input_param
	 */
	function updateCategory($input_param){
		$valueArray = array();
		$valueArray['class_name'] = $input_param['txtClassName'];
		$valueArray['class_state'] = intval($input_param['rdoClassState']);
		$valueArray['class_sort'] = intval($input_param['txtClassSort']);
		$valueArray['class_property'] = intval($input_param['rdoClassProperty']);
		$valueArray['parent_id'] = $input_param['slcParentId'];
		$valueArray['class_id'] = $input_param['class_id'];
		$update_rs = $GLOBALS['db']->UpdateRow($valueArray["class_id"], $valueArray, 'shop_class', 'class_id');
		return $update_rs;
	}
	
	/**
	 * 得到某分类的子类数组
	 *
	 * @param int $parent_id  
	 * @return var[]
	 */
	function _updateCategory($input_param){
		if (is_array($input_param)){
			$value_array = array();
			foreach ($input_param as $k => $v){
				$value_array[$k] = $v;
			}
			$insert_rs = $GLOBALS['db']->UpdateRow($value_array['class_id'],$value_array, 'shop_class', 'class_id');
			return $insert_rs;
		}
		return false;
	}

	/**
	 * 得到某分类的子类数组
	 *
	 * @param int $parent_id  父类ID，一级类的父类ID为0
	 * @param boolen $is_all  是否显示全部的
	 * @return var[]
	 */
	function getCategory($parent_id="",$is_all = false){
		if ($is_all == false){
			$substr = " and class_state='1'";
		}
		if ($parent_id != ""){
			$substr .= " and parent_id='" . $parent_id . "'";
		}
		$substr .= " order by class_sort asc,class_id asc";
		$category_row = $GLOBALS['db']->GetList($substr,$page,'shop_class','*');
		return $category_row;
	}


	/**
	 * 得到某个分类信息
	 *
	 * @param int $input_id 分类ID
	 * @return var[]
	 */
	function getOneCategory($input_id){
		if (intval($input_id) > 0){
			$category = $GLOBALS['db']->GetOneRow(intval($input_id),'shop_class','class_id');
			return $category;
		}else {
			return false;
		}
	}

	/**
	 * 删除商铺分类
	 *
	 * @param var $input_id 分类ID，可以是某个分类ID，也可以是分类ID数组
	 * @return boolen
	 */
	function delOperateCategory($input_id){
		if (is_array($input_id)){
			foreach ($input_id as $value){
				$this->_deleteCategory($value);
			}
		}else{
			$this->_deleteCategory($input_id);
		}
		return true;
	}

	/**
	 * 删除某个商铺分类
	 *
	 * @param int $input_id 商铺分类ID
	 * @return boolen
	 */
	function _deleteCategory($input_id){
		if (intval($input_id) > 0){
			$del_rs= $GLOBALS['db']->DeleOneRow(intval($input_id),'shop_class','class_id');
		}
		return $del_rs;
	}

	/**
	 * 得到某个级别的商铺分类,不同上级的分类之间用$line隔开
	 *
	 * @param int $level  分类的级别 从1开始
	 * @param string $line   分隔线
	 * $ifmerge 是否需要合并‘请选择’选项，默认为1需要
	 * @return var[]
	 */
	function getLevelCategory($level,$line,$cur_level=0,$parent_id=0,$ifmerge=0){
		$cur_level++;
		$category_list = array();
		$category_row = $GLOBALS['db']->GetList("and class_state='1' and parent_id='" . $parent_id . "' order by class_sort asc,class_id asc",$page,'shop_class','*');
		if (is_array($category_row)){
			foreach($category_row as $key=>$value){
				if ($ifmerge == '1') {
					$category_list = array_merge($category_list, array(array('class_name'=>$this->_lang['langSysShopAllClass'],'class_id'=>'')));
				}
				//if ($level == $cur_level){
					if ($parent_id != 0) {
						$value['class_name'] = $line.$value['class_name'];
					}
					$category_list[] = $value;
				//}
				if ($level > $cur_level){
					if ($children = $this->getLevelCategory($level,$line,$cur_level,$value['class_id'])){
						$category_list = array_merge($category_list, $children);
					}
				}
				
			}
		}

		$i = 0 ;

		foreach($category_list as $key=>$val) {
			if (is_array($val)){
				foreach($val as $k=>$v){
					$category_array[$i][$k] = $val[$k];
				}
				$i++;
			}
		}
		return $category_array;
	}


	/**
	 * 得到商铺分类的多维数组
	 *
	 * @param int $parent_id
	 * @param var[] $catearr
	 * @return var[]
	 */
	function getCategoryList($parent_id=0,&$catearr){
		$category_row = $GLOBALS['db']->GetList("and class_state='1' and parent_id='" . $parent_id . "'",$page,'shop_class','*');
		$i = 0;
		foreach ($category_row as $k=>$v){
			if ($parent_id > 0){
				$catearr[count($catearr)-1]['baby'][$i] = $v;
			}else{
				$catearr[count($catearr)] = $v;
			}
			$this->getCategoryList($v['class_id'],$catearr);

			$i++;
		}
		return $catearr;
	}
	
	/**
	 * 更新删除类别下的类别父ID
	 * @author workflow Develop Team
	 * @param  
	 * @return int/bool/object/array
	 */
	function updateShopParentClass($parentId,$new_id){
		$value_array = array();
		$value_array['parent_id'] = $new_id;
		$result = $GLOBALS['db']->UpdateRow($parentId,$value_array,'shop_class','parent_id');
		return $result;
	}
	
	/**
	 * 转移类别下的信息到另一个类别中
	 * @author workflow Develop Team     
	 * @param  $move_array需要转移的ID数组,$to_id转入的类别ID
	 * @return bool
	 */
	function moveShopClass($move_array,$to_id){
		if (intval($to_id) > 0 && !empty($move_array)){
			foreach ($move_array as $k => $v){
				$condition_str[] = "class_id = '" . $v . "'";
			}
			$condition_str = implode(' or ',$condition_str);
			$value_array = array();
			$value_array['parent_id'] = intval($to_id);
			if (substr(trim($condition_str),0,3) == 'and'){
				$condition_str = substr(trim($condition_str),3);
			}
			$result = $GLOBALS['db']->UpdateRows('shop_class',$value_array,$condition_str);
			unset($condition,$condition_str,$value_array);
			return $result;
		}
		return false;
	}
}
?>